package com.lwl.Algorithmic_data_structure.class24滑动窗口;

import java.util.HashSet;

/**
 * @author lwl
 * @Description 给定一个字符串 s ，请你找出其中不含有重复字符的 最长 子串 的长度。
 * @date 2025/8/18 20:40
 */
public class Leetcode03LengthOfLongestSubstring {
    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abcabcbb"));
        System.out.println(lengthOfLongestSubstring("bbbbb"));
        System.out.println(lengthOfLongestSubstring("pwwkew"));
    }

    public static int lengthOfLongestSubstring(String s) {
        HashSet<Character> set = new HashSet<>();
        int res = 0;
        int left = 0;
        for (int right = 0; right < s.length(); right++) {
            while (set.contains(s.charAt(right))) {
                set.remove(s.charAt(left++));
            }
            set.add(s.charAt(right));
            res = Math.max(res, right - left + 1);
        }
        return res;
    }
}
